﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPGClient
{
    public class BufferUtil
    {
        /// <summary>
        /// 获取高8位
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte GetHigh(int value)
        {
            return (byte)((value >> 8) & 0xFF);
        }

        /// <summary>
        /// 获取低8位
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte GetLow(int value)
        {
            return (byte)(value & 0xFF);
        }

        public static int Combine(byte low, byte high)
        {
            return (high<<8) | low;
        }

        public static void IntToBuffer(int value, byte[] buffer, int index)
        {
            buffer[index] = (byte)(value & 0xFF);
            buffer[index + 1] = (byte)((value >> 8) & 0xFF);
        }

        public static int IntFromBuffer(byte[] buffer, int index)
        {
            return BufferUtil.Combine(buffer[index], buffer[index + 1]);
        }

        /// <summary>
        /// 把uint拷贝到buffer中，uint占4byte，32bit
        /// </summary>
        /// <param name="value"></param>
        /// <param name="buffer"></param>
        /// <param name="index"></param>
        public static void UIntToBuffer(uint value, byte[] buffer, int index)
        {
            buffer[index] = (byte)(value & 0xFF);
            uint tempValue = value >> 8;
            buffer[index + 1] = (byte)(tempValue & 0xFF);
            tempValue = value >> 8;
            buffer[index + 2] = (byte)(tempValue & 0xFF);
            tempValue = value >> 8;
            buffer[index + 3] = (byte)(tempValue & 0xFF);
        }

        public static void UIntToBuffer(uint value, byte[] buffer, int index, out int newIndex)
        {
            UIntToBuffer(value, buffer, index);
            newIndex = index + 4;
        }

        /// <summary>
        /// 从buffer中读取一个uint出来，uint占4byte,32bit
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static uint UIntFromBuffer(byte[] buffer, int index)
        {
            uint value = (uint)buffer[index];
            value = (uint)((((uint)buffer[index + 1]) << 8) | value);
            value = (uint)((((uint)buffer[index + 2]) << 8) | value);
            value = (uint)((((uint)buffer[index + 3]) << 8) | value);
            return value;
        }

        public static uint UIntFromBuffer(byte[] buffer, int index, out int newIndex)
        {
            newIndex = index+4;
            return UIntFromBuffer(buffer, index);
        }

        /// <summary>
        /// 把ushort拷贝到buffer中，ushort占2byte，16bit
        /// </summary>
        /// <param name="value"></param>
        /// <param name="buffer"></param>
        /// <param name="index"></param>
        public static void UShortToBuffer(ushort value, byte[] buffer, int index)
        {
            buffer[index] = (byte)(value & 0xFF);
            ushort tempValue = (ushort)(value >> 8);
            buffer[index + 1] = (byte)(tempValue & 0xFF);
        }

        public static void UShortToBuffer(ushort value, byte[] buffer, int index, out int newIndex)
        {
            UShortToBuffer(value, buffer, index);
            newIndex = index + 2;
        }

        public static ushort UShortFromBuffer(byte[] buffer, int index)
        {
            ushort value = (ushort)buffer[index];
            value = (ushort)((((ushort)buffer[index + 1]) << 8) | value);
            return value;
        }

        public static ushort UShortFromBuffer(byte[] buffer, int index, out int newIndex)
        {
            newIndex = index + 2;
            return UShortFromBuffer(buffer, index);
        }

        public static void ByteToBuffer(byte value, byte[] buffer, int index)
        {
            buffer[index] = value;
        }

        public static void ByteToBuffer(byte value, byte[] buffer, int index, out int newIndex)
        {
            buffer[index] = value;
            newIndex = index + 1;
        }

        public static byte ByteFromBuffer(byte[] buffer, int index)
        {
            return buffer[index];
        }

        public static byte ByteFromBuffer(byte[] buffer, int index, out int newIndex)
        {
            newIndex = index + 1;
            return buffer[index];
        }

        /// <summary>
        /// 把字符串填充到buffer中，填充区域是[index, index+length)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="buffer"></param>
        /// <param name="index"></param>
        /// <param name="length"></param>
        public static void StringToBuffer(string value, byte[] buffer, int index, int length)
        {
            byte[] valueBuffer = Encoding.UTF8.GetBytes(value);
            for (int copyIndex = 0; copyIndex < length; copyIndex++)
            {
                if (copyIndex < valueBuffer.Length)
                {
                    buffer[index + copyIndex] = valueBuffer[copyIndex];
                }
                else
                {
                    buffer[index + copyIndex] = 0;
                }
            }
        }

        public static void StringToBuffer(string value, byte[] buffer, int index, int length, out int newIndex)
        {
            StringToBuffer(value, buffer, index, length);
            newIndex = index + length;
        }

        /// <summary>
        /// 将buffer中的[index, index+length)区域的数据转换为字符串
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="index"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string StringFromBuffer(byte[] buffer, int index, int length)
        {
            return Encoding.UTF8.GetString(buffer, index, length).TrimEnd('\0');
        }

        public static string StringFromBuffer(byte[] buffer, int index, int length, out int newIndex)
        {
            newIndex = index + length;
            return StringFromBuffer(buffer, index, length);
        }
    }
}
